//
//  QSShareView.m
//  QSSmarkPark-iOS
//
//  Created by 孙俊 on 2018/3/17.
//  Copyright © 2018年 QiSheng. All rights reserved.
//

#import "QSShareView.h"

#define kContentViewH (kRealValue(230) + kiPhoneXSafeAreaBottomMagin)

@interface QSShareView ()

@property (nonatomic,strong) UIView * contentView;
@property (nonatomic,strong) UIView * blackMaskView;
@property (nonatomic,strong) UILabel * titleLabel;

@property (nonatomic,strong) NSArray * platformImageArray;
@property (nonatomic,strong) NSArray * platformNameArray;

@property (nonatomic,copy) PlatformSelectionBlock platformSelectionBlock;

@end

@implementation QSShareView

+ (void)showShareMenuViewAndPlatformSelectionBlock:(void (^)(UMSocialPlatformType))platformSelectionBlock {
    QSShareView *shareView = [[QSShareView alloc] initWithFrame:kScreenBounds];
    shareView.platformSelectionBlock = platformSelectionBlock;
    [shareView show];
}

- (instancetype)initWithFrame:(CGRect)frame {
    if (self = [super initWithFrame:frame]) {
        self.platformImageArray = @[@"weixin",@"pengy",@"QQ",@"weibo"];
        self.platformNameArray = @[@"微信",@"朋友圈",@"QQ",@"微博"];
        [self createView];
    }
    return self;
}

- (void)createView{
    self.blackMaskView = [[UIView alloc] initWithFrame:self.bounds];
    self.blackMaskView.backgroundColor = [UIColor blackColor];
    [self addSubview:self.blackMaskView];
    
    self.contentView = [[UIView alloc] initWithFrame:CGRectMake(0 , kScreenHeight, kScreenWidth, kContentViewH)];
    self.contentView.backgroundColor = RGBColor(@"f1f1f1");
    self.contentView.layer.cornerRadius = 7;
    [self addSubview:self.contentView];
    //title
    self.titleLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, kRealValue(15), kScreenWidth, kRealValue(16))];
    self.titleLabel.text = @"分享到";
    self.titleLabel.font = [UIFont systemFontOfSize:kRealValue(16)];
    self.titleLabel.textAlignment = NSTextAlignmentCenter;
    [self.contentView addSubview:self.titleLabel];
    
    CGFloat space = kRealValue(32.5);
    CGFloat imageWH = kRealValue(51);
    for (int i = 0; i < self.platformImageArray.count; i++) {
        UIButton * shareBtn = [[UIButton alloc] initWithFrame:CGRectMake(space + i*(space + imageWH), CGRectGetMaxY(self.titleLabel.frame) + kRealValue(35), imageWH, imageWH)];
        shareBtn.tag = i;
        [shareBtn setImage:[UIImage imageNamed:self.platformImageArray[i]] forState:UIControlStateNormal];
        [shareBtn addTarget:self action:@selector(shareClick:) forControlEvents:UIControlEventTouchUpInside];
        [self.contentView addSubview:shareBtn];
        
        UILabel * label = [[UILabel alloc] initWithFrame:CGRectMake(0, CGRectGetMaxY(shareBtn.frame) + kRealValue(13), kRealValue(55), kRealValue(13))];
        label.centerX = shareBtn.centerX;
        label.font = [UIFont systemFontOfSize:kRealValue(13)];
        label.textAlignment = NSTextAlignmentCenter;
        label.text = self.platformNameArray[i];
        [self.contentView addSubview:label];
    }
    //分割线
    UIView * sepView = [[UIView alloc] initWithFrame:CGRectMake(0, kRealValue(177), kScreenWidth, 1)];
    sepView.backgroundColor = RGBColor(@"#dddddd");
    [self.contentView addSubview:sepView];
    //取消按钮
    UIButton * cancelButton = [[UIButton alloc] initWithFrame:CGRectMake(0, CGRectGetMaxY(sepView.frame), kScreenWidth, 53)];
    [cancelButton setBackgroundColor:RGBColor(@"f1f1f1")];
    [cancelButton setTitle:@"取消分享" forState:UIControlStateNormal];
    cancelButton.titleLabel.font = [UIFont systemFontOfSize:kRealValue(16)];
    [cancelButton addTarget:self action:@selector(dissmiss) forControlEvents:UIControlEventTouchUpInside];
    [cancelButton setTitleColor:RGBColor(@"#999999") forState:UIControlStateNormal];
    [self.contentView addSubview:cancelButton];
}

- (void)show{
    [[UIApplication sharedApplication].keyWindow addSubview:self];
    self.blackMaskView.alpha = 0;
    [UIView animateWithDuration:0.3 animations:^{
        self.blackMaskView.alpha = 0.7;
        self.contentView.frame = CGRectMake(0 , kScreenHeight - kContentViewH, kScreenWidth, kContentViewH);
    } completion:^(BOOL finished) {
        
    }];
}

- (void)dissmiss{
    [UIView animateWithDuration:0.3 animations:^{
        self.blackMaskView.alpha = 0;
        self.contentView.frame = CGRectMake(0 , kScreenHeight, kScreenWidth, kContentViewH);
    } completion:^(BOOL finished) {
        [self removeFromSuperview];
    }];
}

- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
    [self dissmiss];
}

- (void)dissmissAndBlockPlatfrom:(UMSocialPlatformType)UMSocialPlatformType {
    [UIView animateWithDuration:0.3 animations:^{
        self.blackMaskView.alpha = 0;
        self.contentView.frame = CGRectMake(0 , kScreenHeight, kScreenWidth, kContentViewH);
    } completion:^(BOOL finished) {
        if(self.platformSelectionBlock){
            self.platformSelectionBlock(UMSocialPlatformType);
        }
        [self removeFromSuperview];
    }];
}

- (void)shareClick:(UIButton *)button{
    switch (button.tag) {
        case 0:
        {
            [self dissmissAndBlockPlatfrom:UMSocialPlatformType_WechatSession];
            break;
        }
        case 1:
        {
            [self dissmissAndBlockPlatfrom:UMSocialPlatformType_WechatTimeLine];
            break;
        }
        case 2:
        {
            [self dissmissAndBlockPlatfrom:UMSocialPlatformType_QQ];
            break;
        }
        case 3:
        {
            [self dissmissAndBlockPlatfrom:UMSocialPlatformType_Sina];
            break;
        }
        default:
            break;
    }
}

- (void)dealloc{
    DLog(@"%@ dealloc",[self class]);
}

@end
